#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk


class GuiApp:
    def __init__(self, master=None):
        # build ui
        self.main = tk.Tk() if master is None else tk.Toplevel(master)
        self.main.configure(height=420, width=600)
        self.main.title("Video Downloader")
        labelframe_1 = ttk.Labelframe(self.main)
        labelframe_1.configure(height=150, text='Video Analyzer', width=500)
        self.combobox_ports = ttk.Combobox(labelframe_1)
        self.port_name = tk.StringVar()
        self.combobox_ports.configure(textvariable=self.port_name, width=20)
        self.combobox_ports.grid(column=1, padx=5, pady=10, row=0, sticky="w")
        self.entry_url = ttk.Entry(labelframe_1)
        self.url = tk.StringVar()
        self.entry_url.configure(textvariable=self.url, width=50)
        self.entry_url.grid(column=1, padx=5, pady=10, row=1, sticky="w")
        label_1 = ttk.Label(labelframe_1)
        label_1.configure(text='Select Analyzing Port:')
        label_1.grid(column=0, padx=5, pady=10, row=0, sticky="e")
        label_2 = ttk.Label(labelframe_1)
        label_2.configure(text='Input Video Address:')
        label_2.grid(column=0, padx=5, pady=10, row=1, sticky="e")
        self.button_play = ttk.Button(labelframe_1)
        self.button_play.configure(text='PLAY', width=15)
        self.button_play.grid(column=1, padx=50, pady=10, row=2, sticky="w")
        labelframe_1.grid(column=0, row=0)
        labelframe_1.grid_anchor("center")
        self.labelframe_2 = ttk.Labelframe(self.main)
        self.labelframe_2.configure(
            height=240, text='Video Downloader', width=500)
        label_3 = ttk.Label(self.labelframe_2)
        label_3.configure(text='Video Name:')
        label_3.grid(
            column=0,
            columnspan=2,
            padx=5,
            pady=10,
            row=0,
            sticky="e")
        label_4 = ttk.Label(self.labelframe_2)
        label_4.configure(text='(m3u8 / ts) Address:')
        label_4.grid(
            column=0,
            columnspan=2,
            padx=5,
            pady=10,
            row=1,
            sticky="e")
        label_5 = ttk.Label(self.labelframe_2)
        label_5.configure(text='Threads:')
        label_5.grid(column=0, padx=5, pady=10, row=2, sticky="e")
        label_6 = ttk.Label(self.labelframe_2)
        label_6.configure(text='Time Out:')
        label_6.grid(column=0, padx=5, pady=10, row=3, sticky="e")
        self.entry_name = ttk.Entry(self.labelframe_2)
        self.video_name = tk.StringVar()
        self.entry_name.configure(textvariable=self.video_name, width=25)
        self.entry_name.grid(column=2, padx=5, pady=10, row=0, sticky="w")
        self.entry_address = ttk.Entry(self.labelframe_2)
        self.address = tk.StringVar()
        self.entry_address.configure(textvariable=self.address, width=50)
        self.entry_address.grid(
            column=2,
            columnspan=2,
            padx=5,
            pady=10,
            row=1,
            sticky="w")
        self.entry_threads = ttk.Entry(self.labelframe_2)
        self.threads = tk.IntVar()
        self.entry_threads.configure(
            justify="center",
            textvariable=self.threads,
            width=10)
        self.entry_threads.grid(column=1, padx=5, pady=10, row=2, sticky="w")
        self.entry_timeout = ttk.Entry(self.labelframe_2)
        self.time_out = tk.IntVar()
        self.entry_timeout.configure(
            justify="center",
            textvariable=self.time_out,
            width=10)
        self.entry_timeout.grid(column=1, padx=5, pady=10, row=3, sticky="w")
        self.entry_ffmpeg = ttk.Entry(self.labelframe_2)
        self.ff_address = tk.StringVar()
        self.entry_ffmpeg.configure(textvariable=self.ff_address, width=40)
        self.entry_ffmpeg.grid(
            column=1,
            columnspan=2,
            padx=5,
            pady=10,
            row=4,
            sticky="w")
        labelframe_4 = ttk.Labelframe(self.labelframe_2)
        labelframe_4.configure(
            height=100,
            text='Applicable to .ts only',
            width=150)
        label_7 = ttk.Label(labelframe_4)
        label_7.configure(text='N-Digit:')
        label_7.grid(column=0, padx=5, pady=10, row=0, sticky="e")
        label_8 = ttk.Label(labelframe_4)
        label_8.configure(text='N-Start:')
        label_8.grid(column=0, padx=5, pady=10, row=1, sticky="e")
        self.entry_ndigit = ttk.Entry(labelframe_4)
        self.n_digit = tk.IntVar()
        self.entry_ndigit.configure(
            justify="center",
            textvariable=self.n_digit,
            width=10)
        self.entry_ndigit.grid(column=1, padx=5, pady=10, row=0, sticky="w")
        self.entry_nstart = ttk.Entry(labelframe_4)
        self.n_start = tk.IntVar()
        self.entry_nstart.configure(
            justify="center",
            textvariable=self.n_start,
            width=10)
        self.entry_nstart.grid(column=1, padx=5, pady=10, row=1, sticky="e")
        labelframe_4.grid(column=3, padx=5, pady=10, row=2, rowspan=3)
        labelframe_4.grid_anchor("center")
        self.checkbutton_FFmpeg = ttk.Checkbutton(self.labelframe_2)
        self.FFmpeg = tk.BooleanVar()
        self.checkbutton_FFmpeg.configure(text='FFmpeg', variable=self.FFmpeg)
        self.checkbutton_FFmpeg.grid(
            column=0, padx=5, pady=10, row=4, sticky="e")
        self.button_open = ttk.Button(self.labelframe_2)
        self.button_open.configure(text='Open Folder', width=15)
        self.button_open.grid(column=2, row=3)
        self.button_download = ttk.Button(self.labelframe_2)
        self.button_download.configure(text='DOWNLOAD', width=15)
        self.button_download.grid(column=2, ipady=5, row=2)
        self.button_select = ttk.Button(self.labelframe_2)
        self.button_select.configure(text='Select m3u8 File')
        self.button_select.grid(column=3, row=0)
        self.labelframe_2.grid(column=0, row=1)
        self.labelframe_2.grid_anchor("center")
        self.main.grid_anchor("center")
        self.main.rowconfigure("all", pad=20)
        self.main.columnconfigure("all", pad=30)

        # Main widget
        self.mainwindow = self.main

    def run(self):
        self.mainwindow.mainloop()


if __name__ == "__main__":
    app = GuiApp()
    app.run()
